﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;

namespace Modules
{
    /// <summary>
    /// 图像防盗链
    /// </summary>
    public class JpgHandler : IHttpHandler
    {
        public void ProcessRequest(HttpContext context)
        {
            // 获取文件服务器端物理路径 
            string FileName = context.Server.MapPath(context.Request.FilePath);
            // 如果UrlReferrer为空，则显示一张默认的禁止盗链的图片 
            if (context.Request.UrlReferrer == null || context.Request.UrlReferrer.Host == null)
            {
                context.Response.ContentType = "image/gif";
                context.Response.WriteFile("/error.gif");
            }
            else
            {
                // 如果 UrlReferrer中不包含自己站点主机域名，则显示一张默认的禁止盗链的图片 
                if (context.Request.UrlReferrer.Host.IndexOf("localhost") >
            0)
                {
                    context.Response.ContentType = "image/gif";
                    context.Response.WriteFile(FileName);
                }
                else
                {
                    context.Response.ContentType = "image/gif";
                    context.Response.WriteFile("/error.gif");
                }
            }
        }

        public bool IsReusable
        {
            get { return true; }
        }
    }
}
